package com.concerneyes.glasses.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.concerneyes.CEApp;
import com.concerneyes.R;

import java.util.List;

import http.JsonMap;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class GlassesAdapter extends BaseAdapter {
    private List<JsonMap> datas;
    private Context context;
    int mSelect = 0;

    public GlassesAdapter(Context context, List<JsonMap> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void changeSelected(int positon) { //刷新方法
        if (positon != mSelect) {
            mSelect = positon;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public JsonMap getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.glasses_price_item_view, null);
            holder.priceItem = (TextView) convertView.findViewById(R.id.price_item);
            holder.iv_select = (ImageView) convertView.findViewById(R.id.iv_select);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }
        String deposit = datas.get(position).getString("deposit");
        String substring = deposit.substring(0, deposit.length() - 3);
        holder.priceItem.setText(substring + "元");

        if (mSelect == position) {
            holder.priceItem.setTextColor(CEApp.getInstance().getResources().getColor(R.color.black_three));
            holder.iv_select.setVisibility(View.VISIBLE);
        } else {
            holder.priceItem.setTextColor(CEApp.getInstance().getResources().getColor(R.color.black_three_five));
            holder.iv_select.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    class viewHolder {
        TextView priceItem;
        ImageView iv_select;
    }
}
